package cn.itcast.utils;

import cn.itcast.service.UserService;
import cn.itcast.service.serviceImp.UserServiceImp;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author: cuisensen
 * data: 2018/6/12.
 * time: 18:49.
 */
public class UserServiceProxy implements InvocationHandler{
    private UserServiceImp us;
    public UserServiceProxy(UserServiceImp us){
        this.us=us;
    }
    public UserService getUserService(){
        return (UserServiceImp)Proxy.newProxyInstance(UserServiceProxy.class.getClassLoader(),us.getClass().getInterfaces(),this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("开启事务");
        Object obj=method.invoke(us,args);
        System.out.println("关闭事物");
        return obj;
    }
}
